package com.tiancheng.trade.payment.util;

import com.tiancheng.trade.commom.core.enums.BaseEnum;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Arrays;

/**
 * 枚举工具类
 *
 * @author kellen
 * @date 2024/3/4
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class EnumUtil {

    /**
     * 根据code获取枚举对象
     *
     * @param enumClass {@link Class<T>}
     * @param code      {@link Integer}
     * @return {@link T}
     */
    public static <T extends BaseEnum> T getEnumByCode(Class<T> enumClass, Integer code) {
        return Arrays.stream(enumClass.getEnumConstants()).filter(d -> d.getCode() == code).findFirst().orElse(null);
    }

    /**
     * 根据code获取枚举对象
     *
     * @param enumClass {@link Class<T>}
     * @param code      {@link String}
     * @return {@link T}
     */
    public static <T extends BaseEnum> T getEnumByCode(Class<T> enumClass, String code) {
        return Arrays.stream(enumClass.getEnumConstants()).filter(d -> d.getCode().equals(code)).findFirst().orElse(null);
    }

    /**
     * 根据code获取枚举对象，忽略code的大小写
     *
     * @param enumClass {@link Class<T>}
     * @param code      {@link String}
     * @return {@link T}
     */
    public static <T extends BaseEnum> T getEnumByCodeIgnoreCase(Class<T> enumClass, String code) {
        return Arrays.stream(enumClass.getEnumConstants()).filter(d -> d.getCode().toString().equalsIgnoreCase(code)).findFirst().orElse(null);
    }
}
